package com.hthy.futures.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;

/**
 *
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/3/27
 */
public class AnimationLoader {
    public static AnimationSet getInAnimation(Context context) {
        AnimationSet in = new AnimationSet(context, null);

        AlphaAnimation alpha = new AlphaAnimation(0.0f, 1.0f);
        alpha.setDuration(90);

        ScaleAnimation scale1 = new ScaleAnimation(0.8f, 1.05f, 0.8f, 1.05f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale1.setDuration(135);

        ScaleAnimation scale2 = new ScaleAnimation(1.05f, 0.95f, 1.05f, 0.95f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale2.setDuration(105);
        scale2.setStartOffset(135);

        ScaleAnimation scale3 = new ScaleAnimation(0.95f, 1f, 0.95f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale3.setDuration(60);
        scale3.setStartOffset(240);

        in.addAnimation(alpha);
        in.addAnimation(scale1);
        in.addAnimation(scale2);
        in.addAnimation(scale3);

        return in;
    }

    public static AnimationSet getOutAnimation(Context context) {
        AnimationSet out = new AnimationSet(context, null);
        AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
        alpha.setDuration(150);
        ScaleAnimation scale = new ScaleAnimation(1.0f, 0.6f, 1.0f, 0.6f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setDuration(150);
        out.addAnimation(alpha);
        out.addAnimation(scale);
        return out;
    }


    /**
     * 点击带有动画的睁开
     *
     * @param hiddenView
     */
    public static void magiceShow(Context context,View hiddenView,int distance) {
        // 获取像素密度
        float mDensity = context.getResources().getDisplayMetrics().density;
        // 获取布局的高度
        int mHiddenViewMeasuredHeight = (int) (mDensity * 240 + 0.5)+distance;   // 如果多的话得加点距离，切换到挂单item
        if (hiddenView.getVisibility() == View.GONE) {
            // 打开动画
            AnimationLoader.animateOpen(hiddenView,mHiddenViewMeasuredHeight);
        } else {
            // 关闭动画
            AnimationLoader.animateClose(hiddenView);
        }
    }

    /**
     * 点击带有动画的睁开
     *
     * @param hiddenView
     */
    public static void magiceShow3(Context context,View hiddenView) {
        // 获取像素密度
        float mDensity = context.getResources().getDisplayMetrics().density;
        // 获取布局的高度
        int mHiddenViewMeasuredHeight = (int) (mDensity * 110 + 20);   // 如果多的话得加点距离，切换到挂单item
        if (hiddenView.getVisibility() == View.GONE) {
            // 打开动画
            AnimationLoader.animateOpen(hiddenView,mHiddenViewMeasuredHeight);
        } else {
            // 关闭动画
            AnimationLoader.animateClose(hiddenView);
        }
    }

    /**
     * 点击带有动画的睁开
     *
     * @param hiddenView
     */
    public static void magiceShow2(Context context,View hiddenView) {
        // 获取像素密度
        float mDensity = context.getResources().getDisplayMetrics().density;
        // 获取布局的高度
        int mHiddenViewMeasuredHeight = (int) (mDensity * 240 + 0.5)+200;   // 如果多的话得加点距离，切换到挂单item
        if (hiddenView.getVisibility() == View.GONE) {
            // 打开动画
            AnimationLoader.animateOpen(hiddenView,mHiddenViewMeasuredHeight);
        } else {
            // 关闭动画
            AnimationLoader.animateClose(hiddenView);
        }
    }

    public static void animateOpen(final View view, int hiddenViewMeasuredHeight) {
        view.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(
                view,
                0,
                hiddenViewMeasuredHeight);
        animator.start();
    }

    public static void animateClose(final View view) {
        int origHeight = view.getHeight();

        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animator.start();
    }

    private static ValueAnimator createDropAnimator(
            final View view, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(
                new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        int value = (Integer) valueAnimator.getAnimatedValue();
                        ViewGroup.LayoutParams layoutParams =
                                view.getLayoutParams();
                        layoutParams.height = value;
                        view.setLayoutParams(layoutParams);
                    }
                });
        return animator;
    }

}
